package 面试;

import java.io.*;

public class 计算文件中单词数 {
    public static void main(String[] args) {
        File file = new File("pw.txt");
        try(
                BufferedInputStream bis = new BufferedInputStream(
                        new FileInputStream(file))
//                BufferedReader br = new BufferedReader(
//                        new InputStreamReader(
//                                new BufferedInputStream(
//                                        new FileInputStream(file)
//                                )
//                        )
//                )
        ){
            int read;
            int count = 0;
            char per=' ',cur;
            while ((read = bis.read()) != -1){
                cur = (char) read;
                if((cur+"").matches("[^a-zA-Z]") && (per+"").matches("[a-zA-Z]")){//只有当前读到的不是字母，且上一个字符是字母，才判断为一个单词
                    count++;
                }
                per = cur;
                System.out.println(cur);
            }
            System.out.println("有" + count + "个单词");
        }catch (IOException e){
            System.out.println(e);
        }

    }
}
